package com.xpu.cos.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xpu.cos.entity.User;
import com.xpu.cos.mapper.UserMapper;
import com.xpu.cos.result.Result;
import com.xpu.cos.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpu.cos.utils.TokenUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ambitionjh
 * @since 2024-01-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public Result addUser(User user) {
        boolean saved = save(user);
        if(saved){
            return Result.ok(saved);
        }else {
            return Result.fail(saved);
        }
    }

    @Override
    public Result updateUser(User user) {
        boolean updated = updateById(user);
        if(updated){
            return Result.ok(updated);
        }else {
            return Result.fail(updated);
        }
    }

    @Override
    public Result getUserById(Integer id) {
        User user = getById(id);
        if(user!=null)
            return Result.ok(user);
        else
            return Result.fail();
    }

    @Override
    public Result deleteUser(Integer id) {
        boolean removed = removeById(id);
        if(removed){
            return Result.ok(removed);
        }else {
            return Result.fail(removed);
        }
    }

    @Override
    public Boolean getUserByUsername(User user) {
        String username = user.getUsername();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        User userTemp = getOne(wrapper);
        if(userTemp==null){
            return true;
        }
        return false;
    }

    @Override
    public User login(String name, String pwd) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,name).eq(User::getPassword,pwd);
        User user = getOne(wrapper);
        return user;
    }
}
